package cn.edu.scau.cmi.yanweijie.client;

import java.awt.TextArea;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import com.sun.jndi.url.iiopname.iiopnameURLContextFactory;

import cn.edu.scau.cmi.yanweijie.adapter.SortUtil;
import cn.edu.scau.cmi.yanweijie.adapter.Student;
import cn.edu.scau.cmi.yanweijie.adapter.Target;
import cn.edu.scau.cmi.yanweijie.interProgramming.Chairman;
import cn.edu.scau.cmi.yanweijie.interProgramming.Marshal;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

public class AdapterClient  extends Application implements EventHandler<ActionEvent>  {
	static List<Student> stuList = new ArrayList<Student>();
	
	public static void main(String arg[]) {
	//生成一个学生序列，并输出
	for(int i =0 ;i<10;i++) {
		Student tempStudent = new Student();
		Random random = new Random();
		Integer idInteger = random.nextInt(1000);
		tempStudent.setStudentIdInteger(idInteger);
		tempStudent.setStudentName("Student"+i);
		stuList.add(tempStudent);
		System.out.println(tempStudent.toString());
	}
	launch(arg);
	}

    String resultString = null ;

    TextField textField = new TextField();
    TextField textField1 = new TextField();

    static Integer INDEX_ONE = 60;
    static Integer INDEX_TWO = 200;
    
    
    Label dis1 = new Label();
    Label dis2 = new Label();
    Label dis3 = new Label();

    Button button;
    Button button1;
    Button button2;
    Button button3;
    Button button4;

    GridPane pane = new GridPane();

    public void start(Stage primaryStage) throws Exception{

        button = new Button("输入学生姓名");
        button1 = new Button("输入学生学号，格式是3位，如000,111,222");
        button2 = new Button("添加到名字序列");
        button3 = new Button("排序1");
        button4 = new Button("排序2");
        button.setOnAction(this);
        button1.setOnAction(this);
        button2.setOnAction(this);
        button3.setOnAction(this);
        button4.setOnAction(this);

        Label nameLabel = new Label();
        nameLabel.setText("姓名:");
        
        Label idLabel = new Label();
        idLabel.setText("学号:");
        
        pane.add(nameLabel, 0, 0);
        pane.add(textField, 10, 0);
        
        pane.add(idLabel, 20, 0);
        pane.add(textField1, 30, 0);

        pane.add(button2, 40, 0);
        pane.add(button3, 50, 0);
        pane.add(button4, 60, 0);
        
        pane.add(dis1, 0, 30);
        
        Scene scene = new Scene(pane, 800, 800);

        primaryStage.setScene(scene);
        primaryStage.show();
    }


    @Override
    public void handle(ActionEvent event) {
    	if(event.getSource()==button2) {
    		Student tempStudent  = new Student();
    		tempStudent.setStudentIdInteger(Integer.valueOf(textField1.getText()));
    		tempStudent.setStudentName(textField.getText());
    		stuList.add(tempStudent);
    		
    		System.out.println("\n\n\n\n\n");
    		resultString="-------------------展示原有的学生序列------------------------------\n";
    		for(int i =0;i<stuList.size();i++) {
    			resultString+= stuList.get(i).toString();
    			resultString+="\n";
    		}
    		dis1.setText(resultString);
    	}
    	
    	//button3代表的是类适配器
    	if (event.getSource()==button3) {
    		Target sortUtil = new SortUtil();
    		System.out.println("\n\n\n\n\n");
    		sortUtil.sort(stuList);
    		resultString="-------------------正在使用类适配器进行sort，升序------------------------------\n";
    		for(int i =0;i<stuList.size();i++) {
    			resultString+= stuList.get(i).toString();
    			resultString+="\n";
    		}
    		dis1.setText(resultString);
		}
//    	button4代表的是对象适配器
    	if (event.getSource()==button4) {
    		SortUtil.sort1(stuList);
    		resultString="-------------------正在使用对象适配器进行sort,降序-----------------------------\n";
    		for(int i =0;i<stuList.size();i++) {
    			resultString+= stuList.get(i).toString();
    			resultString+="\n";
    		}
    		dis1.setText(resultString);
		}
    }
    
    public Map<Integer, String> sortByList(Map<Integer,String>map ,List<Integer> list){
    	Map<Integer, String> map2 = new HashMap<Integer, String>();
    	
    	for (Integer integer : list) {
    		
    		String nameString = map.get(integer);
    		map2.put(integer, nameString);
		}
    	
    	return map2;
    }
	
}